﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ScrumDashboard.Library.Model
{
    /// <summary>
    /// A user in TFS
    /// </summary>
    public class User
    {
        public User(string displayName, string accountName, string domain)
        {
            DisplayName = displayName;
            AccountName = accountName;
            Domain = domain;
            Initials = String.Empty;

            SetInitials();
        }

        public string DisplayName { get; private set; }
        public string AccountName { get; private set; }
        public string Domain { get; private set; }
        public string Initials { get; private set; }

        public static User Empty = new User("","","");

        public override string ToString()
        {
            return DisplayName.ToString();
        }

        private void SetInitials()
        {
            switch (ConfigurationManager.AppSettings["tfsUserInitials"])
            {
                case "AccountName":
                    Initials = AccountName;
                    break;
                
                default:
                    if (String.IsNullOrEmpty(DisplayName))
                        break;

                    if (DisplayName.Contains(" "))
                    {
                        string[] parts = DisplayName.Split(' ');
                        foreach (string part in parts)
                        {
                            Initials += part.Length == 0 ? String.Empty : part.Substring(0, 1);
                        }
                    }
                    else
                    {
                        Initials = DisplayName.Substring(0, 2);
                    }
                    break;
            }
        }
    }
}
